<?php

/**
 * 73YC 限时特购
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

if (!function_exists("htmlspecialchars_decode"))
{
    function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT)
    {
        return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
    }
}


require(dirname(__FILE__) . '/includes/init.php');

// $_REQUEST = array_merge($_REQUEST, addslashes_deep($string));
// $_REQUEST['act'] = !empty($_REQUEST['act']) ? trim($_REQUEST['act']) : '';

    
    assign_template();
    $position = assign_ur_here();
    $smarty->assign('page_title', $position['title']);    // 页面标题

    $now_time  = time();//当前时间
    $test_time = time();//测试时间
    // $test_time = "1415635200";//测试时间

    //根据当前时间判断属于第几场
    if($test_time<='1416067200'){
        $buy_num=1;
    }
    elseif ($test_time>='1416067200' && $test_time<'1416096000') {
        $buy_num=2;
    }
    elseif ($test_time>'1416096000' && $test_time<'1416114000') {
        $buy_num=3;
    }
    elseif ($test_time>'1416114000' && $test_time<'1416132000') {
        $buy_num=4;
    }
    elseif ($test_time>'1416132000') {
        $buy_num=5;
    }
    // 判断是否点击了场数
    $now_num =! empty($_REQUEST['buy_num'])?$_REQUEST['buy_num']:$buy_num;
    switch ($now_num) {
        case '1':
            $time='1416060000';
            break;
        case '2':
            $time='1416067200';
            break;
        case '3':
            $time='1416096000';
            break;
        case '4':
            $time='1416114000';
            break;
        case '5':
            $time='1416132000';
            break;
        
        default:
            $time=time();
            break;
    }
    //如果当前点击场数与当前进行场数相同则显示最后疯抢
    if(($_REQUEST['buy_num'] == $buy_num)||empty($_REQUEST['buy_num'])){
        //最后疯抢 
        $sql = 'SELECT g.goods_id,  g.sku, g.goods_name, g.promote_start_date,g.promote_end_date, g.goods_thumb , g.promote_price,g.market_price,sum(gs.goods_number) AS goods_stock ' .
        ' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
        ' LEFT JOIN '.$GLOBALS['ecs']->table('goods_stock') . ' AS gs ON gs.goods_id = g.goods_id' .
        ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
        " AND g.promote_start_date >= '1416060000' AND g.promote_start_date < '$test_time' AND g.promote_start_date != '$time'".
        " GROUP BY g.goods_id ";
        $last_goods=$GLOBALS['db']->getAll($sql);        
        foreach ($last_goods AS $idx => $row)
        {   
            $last_goods[$idx]['end'] = ($row['promote_end_date']-$test_time)<0?1:0;
            $last_goods[$idx]['url']                = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
        }
    }
    //第一场都没有开始

        //第一场商品
        $sql = 'SELECT g.goods_id,  g.sku, g.goods_name, g.promote_start_date,g.promote_end_date, g.goods_thumb , g.promote_price,g.market_price ,sum(gs.goods_number) AS goods_stock ' .
        ' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
        ' LEFT JOIN '.$GLOBALS['ecs']->table('goods_stock') . ' AS gs ON gs.goods_id = g.goods_id' .
        ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
        " AND g.promote_start_date = '$time'".
        " GROUP BY g.goods_id ";
        $first_goods = $GLOBALS['db']->getAll($sql);
        foreach ($first_goods AS $idx => $row)
        {   
            $first_goods[$idx]['end'] = ($row['promote_end_date']-$test_time)<0?1:0;
            $first_goods[$idx]['promote_date'] = time_start(($row['promote_start_date']-$test_time)) ;
            $first_goods[$idx]['lave_time'] =intval($row['promote_start_date']-$test_time);
            $first_goods[$idx]['url']                = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
        }

    //预告
    $sql = 'SELECT g.goods_id, g.sku, g.goods_name, g.promote_start_date, g.goods_thumb , g.promote_price,g.market_price ' .
    'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
    'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
    " AND g.promote_start_date > '$test_time' AND g.promote_start_date !=$time ";

    $pre_goods = $GLOBALS['db']->getAll($sql);
    foreach ($pre_goods AS $idx => $row)
    {   
        $pre_goods[$idx]['promote_date'] = time_start(($row['promote_start_date']-time())) ;
        $pre_goods[$idx]['lave_time'] =intval($row['promote_start_date']-$test_time);
        $pre_goods[$idx]['url']                = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
    }


    //距离第一场剩余时间
    $open_time=1431532800-time();
    // echo "<pre>";
    // print_r();
    // echo "</pre>";
    // exit;
    
    $smarty->assign('open_time', ($open_time*10));
    $smarty->assign('pic_time', get_time($open_time));
    $smarty->assign('open_time_t', time_start($open_time));

    // foreach ($first_goods as $key => $value) {
    //     if(time() > $time){
    //         $sql = "SELECT SUM(goods_number) FROM " . $ecs->table('cart') . " WHERE goods_id = $value[goods_id]";
    //         $s1 = $db->getOne($sql) * $value['sku'];
    //         $sql = "SELECT SUM(goods_number) FROM " . $ecs->table('order_goods') . " WHERE goods_id = $value[goods_id] AND add_time >$time";
    //         $s2 = $db->getOne($sql)*$value['sku'];
    //     }
    //     $first_goods[$key]['sumbuy'] = $s1 + $s2;
    // }

    $smarty->assign('first_goods', $first_goods);
    $smarty->assign('pre_goods', $pre_goods);
    $smarty->assign('last_goods', $last_goods);
    $smarty->assign('buy_num', $now_num);
    // $smarty->assign('forecast',get_forecast_goods(10));   
    $smarty->display('limit.dwt');



/*格式化时间戳函数*/
function  time_start($data){
        $Y = '';
        $D = '';
        $H = '';
        $M = '';
        $T = '';
        if(intval($data/3600)!=0){
          $H=intval($data/3600)."时";
        }else if($D!=''){
          $H='0'."时";
        }
        if(intval($data%(3600)/60)!=0){
          $M=intval((($data%(3600))/60))."分";
        }else if($H!=''){
          $M='0'."分";
        }
        if(intval($data%60)!=0){
          $T=intval($data%60)."秒";
        }else if($M!=''){
          $T='0'."秒";
        }
        return $Y.$D.$H.$M.$T;
      }
/*格式话时间*/
function get_time($data){
        $pic_time['hours']=intval($data/3600);
        $pic_time['minutes']=intval((($data%(3600))/60));
        $pic_time['seconds']=intval($data%60);
        $pic_time['seconds_1']="time_".floor($pic_time['seconds']/10);
        $pic_time['seconds_2']="time_".floor($pic_time['seconds']%10);
        $pic_time['minutes_1']="time_".floor($pic_time['minutes']/10);
        $pic_time['minutes_2']="time_".floor($pic_time['minutes']%10);
        $pic_time['hours_1']="time_".floor($pic_time['hours']/10);
        $pic_time['hours_2']="time_".floor($pic_time['hours']%10);

        return $pic_time;


}



/*------------------------------------------------------ */
//-- PRIVATE FUNCTION
/*------------------------------------------------------ */
/**
 *
 *
 * @access public
 * @param
 *
 * @return void
 */
function is_not_null($value)
{
    if (is_array($value))
    {
        return (!empty($value['from'])) || (!empty($value['to']));
    }
    else
    {
        return !empty($value);
    }
}

/**
 * 获得预售商品
 *
 * @access  public
 * @return  array
 */
function get_forecast_goods($num=null)
{
    $time = gmtime();
    $order_type = $GLOBALS['_CFG']['recommend_order'];

    /* 取得促销lbi的数量限制 */
    if(empty($num)){
        $num = get_library_number("recommend_promotion");
    }

    $sql = 'SELECT g.goods_id, g.goods_name, g.promote_start_date, g.goods_thumb, g.goods_img ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
            'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
            " AND g.is_promote = 1 AND g.promote_start_date > '$time'";

    $sql .= $order_type == 0 ? ' ORDER BY g.promote_start_date ASC' : ' ORDER BY rnd';
    $sql .= " LIMIT $num ";
    $result = $GLOBALS['db']->getAll($sql);

    $goods = array();
    foreach ($result AS $idx => $row)
    {
        $goods[$idx]['goods_id']           = $row['goods_id'];
        $goods[$idx]['goods_name']         = $row['goods_name'];
        $goods[$idx]['promote_start_date'] = local_date('Y-m-d H:i',$row['promote_start_date'] ); 
        $goods[$idx]['thumb']              = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $goods[$idx]['goods_img']          = get_image_path($row['goods_id'], $row['goods_img']);
        $goods[$idx]['url']                = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
    }

    return $goods;
}

?>
